#include "adaptivewidgets.h"



AWidget::AWidget(QWidget *parent)
    : AdaptiveWidget<QWidget>{parent}
{
    setStyleSheet(R"(
        AWidget {
            background: transparent;
            color: #FFFFFF;
        }
    )");
}

ADialog::ADialog(QWidget *parent)
    : AdaptiveWidget<QDialog>{parent}
{
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
}

AMainWindow::AMainWindow(QWidget *parent)
    : AdaptiveWidget<QMainWindow>{parent}
{
    setStyleSheet(QString(R"(
        AMainWindow {
            background-color: #2B3033;
        }
        QMenuBar {
            background-color: #000000;
            color: white;
            border: none;
        }
        QMenuBar::item {
            background-color: transparent;
            color: white;
            border-left: 1px solid #5E636B;
            padding-left: %1px;
            padding-right: %1px;
            margin-top: 1px;
            margin-bottom: 1px;
        }
        QMenuBar::item:hover {
            background-color: #3498db;
        }
        QMenuBar::item:selected {
            background-color: #2980b9;
        }
        QMenuBar::item:pressed {
            background-color: #1f6dad;
        }
    )").arg(10 * ratio()));
}

AScrollArea::AScrollArea(QWidget *parent)
    : AdaptiveWidget<QScrollArea>{parent}
{
    setStyleSheet(R"(
        AScrollArea {
            background: transparent;
            border: none;
        }
    )");
}

ATabWidget::ATabWidget(QWidget *parent)
    : AdaptiveWidget<QTabWidget>{parent}
{
    setStyleSheet(QString(R"(
        QTabWidget::pane{
            border: 1px solid #5E636B;
            background-color: #000000;
        }
        QTabBar::tab {
            min-width: %1px;
            height: %2px;
            color: #FFFFFF;
            background-color: #454952;
            border-left: 1px solid #5E636B;
            border-right: 1px solid #5E636B;
            border-top: 1px solid #5E636B;
            border-top-left-radius: %3px;
            border-top-right-radius: %3px;
            margin-left: 0px;
            margin-right: 1px;
        }
        QTabBar::tab:selected{
            background-color: #000000;
        }
    )").arg(100 * ratio()).arg(20 * ratio()).arg(6 * ratio()));
}

int ATabWidget::addTab(QWidget *widget, const QString &title)
{
    return QTabWidget::addTab(widget, QCoreApplication::translate("ATabWidget", title.toUtf8()));
}

AGroupBox::AGroupBox(QWidget *parent)
    : AdaptiveWidget<QGroupBox>{parent}
{
    setStyleSheet(QString(R"(
        AGroupBox {
            border: 1px solid #5E636B;
            background-color: #000000;
        }
    )"));
}

AGroupBox::AGroupBox(const QString &title, QWidget *parent)
    : AdaptiveWidget<QGroupBox>{parent}
{
    setTitle(title);
    setStyleSheet(QString(R"(
        AGroupBox {
            border-top: %1px solid #5E636B;
            border-left: 1px solid #5E636B;
            border-right: 1px solid #5E636B;
            border-bottom: 1px solid #5E636B;
            background-color: #000000;
        }
        AGroupBox::title {
            color: #FFFFFF;
            subcontrol-position: top center;
            background-color: #5E636B;
            top: %2px;
            margin-left: -%2px;
        }
    )").arg(20 * ratio()).arg(4 * ratio()));
}

void AGroupBox::setTitle(const QString &title)
{
    QGroupBox::setTitle(QCoreApplication::translate("AGroupBox", title.toUtf8()));
}

ACheckBox::ACheckBox(QWidget *parent)
    : AdaptiveWidget<QCheckBox>{parent}
{
    setFixedSize(DevEnvParam::CTRL_FIX_WIDTH, DevEnvParam::CTRL_FIX_HEIGHT);
    setLayoutDirection(Qt::RightToLeft);
    setStyleSheet(QString(R"(
        ACheckBox {
            background: transparent;
            color: #FFFFFF;
        }
        ACheckBox::indicator {
            width: %1px;
            height: %1px;
            background: transparent;
            image: url(:/img/uncheck.svg);
        }
        ACheckBox::indicator:hover {
            background: transparent;
            border: 1px solid #FFFFFF;
            border-radius: 1px;
        }
        ACheckBox::indicator:checked {
            image: url(:/img/check.svg);
        }
        ACheckBox:disabled {
            color: #999999;
        }
    )").arg(15 * ratio()));
}

ACheckBox::ACheckBox(const QString &text, QWidget *parent)
    : ACheckBox{parent}
{
    setText(text);
}

AComboBox::AComboBox(QWidget *parent)
    : AdaptiveWidget<QComboBox>{parent}
{
    setFixedHeight(DevEnvParam::CTRL_FIX_HEIGHT);
    setStyleSheet(QString(R"(
        AComboBox {
            padding-left: %1px;
            border: 1px solid #FFFFFF;
            border-radius: %1px;
            background: transparent;
            color: #FFFFFF;
        }
        AComboBox::drop-down {
            image: url(:/img/arrow-down.svg);
            padding-right: %1px;
            width: %2px;
        }
        AComboBox::drop-down:hover {
            padding-right: %1px;
            width: %3px;
        }
    )").arg(4 * ratio()).arg(11 * ratio()).arg(12 * ratio()));
}

void AComboBox::addItem(const QString &text, const QVariant &userData)
{
    QComboBox::addItem(QCoreApplication::translate("AComboBox", text.toUtf8()), userData);
}

void AComboBox::addItems(const QStringList &texts)
{
    for (int i = 0; i < texts.size(); ++i) {
        addItem(texts.at(i));
    }
}

ASpinBox::ASpinBox(QWidget *parent)
    : AdaptiveWidget<QSpinBox>{parent}
{
    setKeyboardTracking(false);
    setFixedSize(DevEnvParam::CTRL_FIX_WIDTH, DevEnvParam::CTRL_FIX_HEIGHT);
    setStyleSheet(QString(R"(
        ASpinBox {
            padding-left: %1px;
            border: 1px solid #FFFFFF;
            border-radius: %1px;
            background: transparent;
            color: #FFFFFF;
        }
        ASpinBox::edit-field {
            background: transparent;
            border: none;
        }
        ASpinBox::up-button {
            image: url(:/img/arrow-up.svg);
            padding-right: %1px;
            width: %2px;
        }
        ASpinBox::down-button {
            image: url(:/img/arrow-down.svg);
            padding-right: %1px;
            width: %2px;
        }
        ASpinBox::up-button:hover, ASpinBox::down-button:hover {
            padding-right: %1px;
            width: %3px;
        }
    )").arg(4 * ratio()).arg(8 * ratio()).arg(9 * ratio()));
}

ADoubleSpinBox::ADoubleSpinBox(QWidget *parent)
    : AdaptiveWidget<QDoubleSpinBox>{parent}
{
    setKeyboardTracking(false);
    setFixedSize(DevEnvParam::CTRL_FIX_WIDTH, DevEnvParam::CTRL_FIX_HEIGHT);
    setStyleSheet(QString(R"(
        ADoubleSpinBox {
            padding-left: %1px;
            border: 1px solid #FFFFFF;
            border-radius: %1px;
            background: transparent;
            color: #FFFFFF;
        }
        ADoubleSpinBox::edit-field {
            background: transparent;
            border: none;
        }
        ADoubleSpinBox::up-button {
            image: url(:/img/arrow-up.svg);
            padding-right: %1px;
            width: %2px;
        }
        ADoubleSpinBox::down-button {
            image: url(:/img/arrow-down.svg);
            padding-right: %1px;
            width: %2px;
        }
        ADoubleSpinBox::up-button:hover, ADoubleSpinBox::down-button:hover {
            padding-right: %1px;
            width: %3px;
        }
    )").arg(4 * ratio()).arg(8 * ratio()).arg(9 * ratio()));
}

ASlider::ASlider(QWidget *parent)
    : AdaptiveWidget<QSlider>{parent}
{
    setMinimumSize(30, 150);
    setStyleSheet(QString(R"(
        /* 推子滑块 */
        ASlider::handle:vertical {
            height: %1px;
            margin: -1 -%2px; /* 超出轨道 */
            background: transparent;
            image: url(:/img/slider.svg);
        }
        /* 推子轨道 */
        ASlider::groove:vertical {
            width: %3px;
            background: #5E636B;
            border: 1px solid #5E636B;
            border-radius: %4px;
        }
        /* 悬停状态 */
        ASlider::handle:vertical:hover {
            height: %5px;
        }
        /* 按下状态 */
        ASlider::handle:vertical:pressed {
            height: %5px;
        }
    )").arg(15 * ratio()).arg(12 * ratio()).arg(5 * ratio()).arg(3 * ratio()).arg(14 * ratio()));
}

ALabel::ALabel(QWidget *parent)
    : AdaptiveWidget<QLabel>{parent}
{
    setStyleSheet(QString(R"(
        ALabel {
            color: #FFFFFF;
        }
    )"));
}

ALabel::ALabel(const QString &text, QWidget *parent)
    : ALabel{parent}
{
    setText(text);
}

void ALabel::setText(const QString &text)
{
    QLabel::setText(QCoreApplication::translate("ALabel", text.toUtf8()));
}

APushButton::APushButton(QWidget *parent)
    : AdaptiveWidget<QPushButton>{parent}
{
    setFixedSize(DevEnvParam::CTRL_FIX_WIDTH, DevEnvParam::CTRL_FIX_HEIGHT);
    setStyleSheet(QString(R"(
        APushButton {
            border-radius: %1px;
            border: none;
            background-color: #555759;
            color: #FFFFFF;
        }
        /* 悬停 */
        APushButton:hover {
            background-color: #FFFFFF;
            color: #555759;
        }
        /* 鼠标按下 */
        APushButton:pressed {
            background-color: #EEEEEE;
            color: #555759;
        }
        /* 选中状态 */
        APushButton:checked {
            background-color: #2F8C00;
            color: #FFFFFF;
        }
        /* 选中状态悬停 */
        APushButton:checked:hover {
            background-color: #FFFFFF;
            color: #555759;
        }
        /* 选中状态鼠标按下 */
        APushButton:checked:pressed {
            background-color: #EEEEEE;
            color: #555759;
        }
        /* 禁用状态 */
        APushButton:disabled {
            background-color: #999999;
            color: #CCCCCC;
            opacity: 0.8;
        }
    )").arg(4 * ratio()));
}

APushButton::APushButton(const QString &text, QWidget *parent)
    : APushButton{parent}
{
    setText(text);
}

void APushButton::setText(const QString &text)
{
    QPushButton::setText(QCoreApplication::translate("APushButton", text.toUtf8()));
}

ALineEdit::ALineEdit(QWidget *parent)
    : AdaptiveWidget<QLineEdit>{parent}
{
    setFixedHeight(DevEnvParam::CTRL_FIX_HEIGHT);
    setAlignment(Qt::AlignCenter);
    setStyleSheet(QString(R"(
        ALineEdit {
            border: 1px solid #FFFFFF;
            border-radius: %1px;
            background: transparent;
            color: #FFFFFF;
            padding-left: %2px;
            padding-right: %2px;
        }
    )").arg(4 * ratio()).arg(2 * ratio()));

    connect(this, &ALineEdit::returnPressed, this, &ALineEdit::onEditFinished);
}

ALineEdit::ALineEdit(const QString &contents, QWidget *parent)
    : ALineEdit{parent}
{
    setText(contents);
    m_lastConfirmedText = contents;
}

void ALineEdit::setText(const QString &text)
{
    QLineEdit::setText(text);
    m_lastConfirmedText = text;
}

void ALineEdit::focusOutEvent(QFocusEvent *e)
{
    QLineEdit::focusOutEvent(e);
    onEditFinished();
}

void ALineEdit::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
        onEditFinished();
        e->accept();
        return;
    }
    QLineEdit::keyPressEvent(e);
}

void ALineEdit::onEditFinished()
{
    QString currentText = text();
    if (currentText != m_lastConfirmedText) {
        m_lastConfirmedText = currentText;
        emit textConfirmed(currentText);
    }
}

AHLine::AHLine(QWidget *parent)
    : QFrame{parent}
{
    setFrameShape(QFrame::HLine);
    setFrameShadow(QFrame::Sunken);
    setStyleSheet(QString(R"(
        AHLine {
            border-top: 1px solid #5E636B;
            background: transparent;
        }
    )"));
}



AVLine::AVLine(QWidget *parent)
    : QFrame{parent}
{
    setFrameShape(QFrame::VLine);
    setFrameShadow(QFrame::Sunken);
    setStyleSheet(QString(R"(
        AVLine {
            border-right: none;
            background: transparent;
        }
    )"));
}
